package com.lcxw.hxzbadmin.controller.unioncontroller;

import com.alibaba.fastjson.JSONObject;
import com.lcxw.hxzbadmin.entity.requestbean.CreateUnionReq;
import com.lcxw.hxzbadmin.entity.requestbean.DealUnionRegistReq;
import com.lcxw.hxzbadmin.entity.requestbean.GetUnionMembersReq;
import com.lcxw.hxzbadmin.entity.requestbean.ShutdownUnionReq;
import com.lcxw.hxzbapi.entity.response.Response;
import com.lcxw.hxzbcommon.entity.ErrorCode;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/admin/union")
@Slf4j
public class UnionManagerController {

    @Resource
    UnionRealization unionRealization;


    /**
     * 创建一个工会
     */
    @PostMapping("/create")
    public Response createUnion(@RequestBody CreateUnionReq createUnionReq) {
        String telephone = createUnionReq.getTelephone();
        try {
            return unionRealization.createUnion(telephone);
        } catch (Exception e) {
            return Response.error(ErrorCode.INTERNAL_ERROR, "服务器内部错误，请联系后台管理人员");
        }
    }

    /**
     * 强制解散一个工会
     */
    @PostMapping("/shutdown")
    public Response shutdownUnion(@RequestBody ShutdownUnionReq shutdownUnionReq) {
        int unionId = shutdownUnionReq.getUnionId();
        String shtReason = shutdownUnionReq.getShtReason();
        return unionRealization.shutdownUnion(unionId, shtReason);
    }

    /**
     * 查询工会列表
     */
    @PostMapping("/getList")
    public Response getUnionList(@RequestBody JSONObject requestBody) {
        String telephone = requestBody.getString("telephone");
        int pageNum = requestBody.getIntValue("pageNum");
        int pageSize = requestBody.getIntValue("pageSize");

        return unionRealization.getUnionList(telephone, pageNum, pageSize);
    }

    @PostMapping("/getMembers")
    public Response getUnionMembers(@RequestBody GetUnionMembersReq getUnionMembersReq) {
        int unionId = getUnionMembersReq.getUnionId();
        int pageNum = getUnionMembersReq.getPageNum();
        int pageSize = getUnionMembersReq.getPageSize();
        return unionRealization.getUnionMembers(unionId, pageNum, pageSize);
    }

    @PostMapping("/deal/union/regist")
    public Response dealUnionRegist(@RequestBody DealUnionRegistReq dealUnionRegistReq) {
        System.out.println(dealUnionRegistReq.toString());
        return unionRealization.dealUnionRegist(dealUnionRegistReq);
    }
}
